package cc.zy.base.hr.service.impl;

import cc.zy.base.businesses.entity.College;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.SortUtil;
import cc.zy.base.hr.entity.TrainningBatch;
import cc.zy.base.hr.mapper.TrainningBatchMapper;
import cc.zy.base.hr.service.ITrainningBatchService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 *  培训批次
 *  Service
 *
 * @author pengfeng
 * @date: 2022/3/8 17:23
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class TrainningBatchImpl extends ServiceImpl<TrainningBatchMapper, TrainningBatch> implements ITrainningBatchService {

    @Resource
    private final TrainningBatchMapper trainningBatchMapper;

    private final Integer DRAFT = 0;

    private final Integer EXECUTE = 1;


    @Override
    public IPage<TrainningBatch> listTrainningBatchs(QueryRequest request, TrainningBatch trainningBatch) {
        Page<TrainningBatch> page = new Page<>(request.getPageNum(), request.getPageSize());
        page.setSearchCount(false);
        page.setTotal(baseMapper.countTrainningBatchDetail(trainningBatch));
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return baseMapper.listTrainningBatchDetailPage(page, trainningBatch);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteTrainningBatchs(String[] trainningBatchIds) {
        List<String> list = Arrays.asList(trainningBatchIds);
        // 删除用户
        this.removeByIds(list);
    }


    @Override
    public List<TrainningBatch> listTrainningBatchListNotPage(TrainningBatch trainningBatch) {
        List<TrainningBatch> trainningBatchListNotPage = trainningBatchMapper.listTrainningBatchListNotPage(trainningBatch);
        return trainningBatchListNotPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTrainningBatch(TrainningBatch trainningBatch) {
        trainningBatch.setCreateDate(new Date());
        trainningBatch.setLastModifyDate(new Date());
        this.save(trainningBatch);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTrainningBatch(TrainningBatch trainningBatch) {
        trainningBatch.setLastModifyDate(new Date());
        System.out.println(trainningBatch);
        this.saveOrUpdate(trainningBatch);
    }

    @Override
    public Boolean isRepetitive(String trainningName) {
        //查看数据库中是否有tagName的值
        Integer id = trainningBatchMapper.findId(trainningName);
        return id == null ? true : false;
    }

    @Override
    public TrainningBatch findById(Integer id) {
        return trainningBatchMapper.getById(id);
    }

    @Override
    public void draftToExecute(Integer id) {
        //查出当前批次
        TrainningBatch trainningBatch = trainningBatchMapper.getById(id);

        //修改培训状态
        Integer statusId = trainningBatch.getTrainningStatusId();
        if(statusId == EXECUTE){
            throw new RuntimeException("不能修改已确定为定稿的批次");
        }
        trainningBatch.setTrainningStatusId(EXECUTE);

        //存入数据库
        this.saveOrUpdate(trainningBatch);
    }

}